<template>
    <div class="login">
        <div class="login_box">
            <!-- 头像区 -->
            <div class="avatar_box">
                <img src="../assets/1.jpg">
            </div>

            <!-- 表单区 -->
            <el-form ref="loginFormRef" :model="Form" :rules="rules" class="login_form">
                 
                <!-- 用户名 -->
                <el-form-item prop="username">
                    <el-input v-model="Form.username" prefix-icon="iconfont icon-yonghu"></el-input>
                </el-form-item>

                <!-- 密码区 -->
                <el-form-item prop="password">
                    <el-input type="password" v-model="Form.password"  prefix-icon="iconfont icon-suo"></el-input>
                </el-form-item>

                <!-- 按钮区 -->
                <el-form-item  class="btns">
                    <el-button type="primary" @click="loginForm">登录</el-button>
                    <el-button type="info" @click="resetForm">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
export default {
     data() {
         return {
             //登录表单数组
             Form:{
                 username:'admin',
                 password:'123456'
             },
             //表单验证规则对象
             rules: {
                 //验证用户名是否合法
                username: [
                    { required: true, message: '请输入用户名', trigger: 'blur' },
                    { min: 3, max:10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
                ],
                //验证密码是否合法
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur' },
                    { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
                ],
            }
         }
     },
     methods: {
        resetForm(){
            this.$refs.loginFormRef.resetFields();
        },
        loginForm(){
             this.$refs.loginFormRef.validate(async valid=>{
                 if(!valid)return;
                 const {data:res} = await this.axios.post('login',this.Form);
                 if(res.meta.status==200){
                     this.$message({
                        message: '登录成功',
                        type: 'success'
                    });
                    window.sessionStorage.setItem('token',res.data.token)
                    this.$router.push('/home')
                 }else{
                     this.$message.error('登录失败');
                 }
             })
        }
     },
}
</script>

<style scoped lang="less">
    .login{
        
        .login_box{
            width: 450px;
            height: 300px;
            position: absolute;
            top: 50%;
            left: 50%;
            background: #fff;
            border-radius: 3px;
            transform: translate(-50%,-50%);

            .login_form{
                position: absolute;
                bottom: 0;
                width: 100%;
                padding: 0 20px;
                box-sizing: border-box;
            }


            .btns{
                display: flex;
                justify-content: flex-end;
            }

            .avatar_box{
                height: 130px;
                width: 130px;
                border-radius: 50%;
                padding: 10px;
                box-shadow: 0 0 10px #ddd;
                position: absolute;
                left: 50%;
                transform: translate(-50%,-50%);
                background: #fff;

                img{
                    height: 100%;
                    width: 100%;
                    border: 1px solid #eee;
                    border-radius: 50%;
                    background: #eee;
                }
            }
        }
    }
</style>